<template>
  <div>
    <!-- 基础题库 -->
    <div class="box-card">
      <div class="box-header">
        <div class="box-top">
          <div>说明：目前支持学科和关键字条件筛选</div>
          <el-button
            type="success"
            class="header-right"
            @click="$router.push('/questions/new')"
            ><i class="el-icon-edit"></i> 新增试题</el-button
          >
        </div>
        <el-form :model="form" ref="form" label-width="80px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select
                  v-model="form.subjectID"
                  placeholder="请选择"
                  style="width: 250px"
                >
                  <el-option
                    v-for="item in subject"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录">
                <el-select
                  v-model="form.catalogID"
                  placeholder="请选择"
                  style="width: 250px"
                >
                 <el-option label="无数据" value="shanghai"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select
                  v-model="form.tags"
                  placeholder="请选择"
                  style="width: 250px"
                >
                  <el-option label="无数据" value="shanghai"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input
                  v-model="form.keyword"
                  placeholder="根据题干搜索"
                  style="width: 250px"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="试题类型">
                <el-select
                  v-model="form.questionType"
                  placeholder="请选择"
                  style="width: 250px"
                >
                  <el-option label="单选" :value="1"></el-option>
                  <el-option label="多选" :value="2"></el-option>
                  <el-option label="简答" :value="3"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度">
                <el-select
                  v-model="form.difficulty"
                  placeholder="请选择"
                  style="width: 250px"
                >
                  <el-option label="简单" :value="1"></el-option>
                  <el-option label="一般" :value="2"></el-option>
                  <el-option label="困难" :value="3"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向">
                <el-select
                  v-model="form.direction"
                  placeholder="请选择"
                  style="width: 250px"
                >
                  <el-option
                    v-for="item in direction"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人">
                <el-select
                  v-model="form.creatorID"
                  placeholder="请选择"
                  style="width: 250px"
                >
                  <el-option
                    v-for="item in user"
                    :key="item.id"
                    :label="item.username"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row justify="space-between">
            <el-col :span="6">
              <el-form-item label="题目备注">
                <el-input
                  v-model="form.remarks"
                  style="width: 250px"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input
                  v-model="form.shortName"
                  style="width: 250px"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="城市">
                <el-select
                  placeholder="请选择"
                  v-model="form.provinces"
                  @change="handleProvince"
                  style="width: 120px; margin-right: 10px"
                >
                  <el-option
                    v-for="item in citySelect.province"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
                <el-select
                  placeholder="请选择"
                  v-model="form.city"
                  style="width: 120px"
                >
                  <el-option
                    v-for="item in citySelect.cityDate"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4" class="cols">
              <el-button class="header-btn cols-btn" @click="clearInput"
                >清空</el-button
              >
              <el-button type="primary" class="header-btn" @click="searchInput"
                >搜索</el-button
              >
            </el-col>
          </el-row>
        </el-form>

        <div class="header-content">
          <i class="el-icon-info"></i>
          <span>数据一共{{ total }}条</span>
        </div>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{ background: '#f4f4f5' }"
      >
        <el-table-column prop="number" label="试题编号" align="center">
        </el-table-column>
        <el-table-column prop="subject" label="学科" align="center">
        </el-table-column>
        <el-table-column prop="catalog" label="目录" align="center">
        </el-table-column>
        <el-table-column prop="questionType" label="题型" align="center">
          <template #default="{ row }">
            <span>{{
              row.questionType === '1'
                ? '单选'
                : row.questionType === '2'
                ? '多选'
                : '简答'
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="question" label="题干" align="center">
          <template #default="{ row }"
            ><span v-html="row.question"></span
          ></template>
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" align="center">
        </el-table-column>
        <el-table-column prop="difficulty" label="难度" align="center">
          <template #default="{ row }">
            <span>{{
              row.difficulty === '1'
                ? '简单'
                : row.difficulty === '2'
                ? '一般'
                : '困难'
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="录入人" align="center">
        </el-table-column>
        <el-table-column label="操作" width="200px" align="center">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-view"
              circle
              class="edit1"
              @click="openDialog(row)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              circle
              class="edit4"
              @click="$router.push(`/questions/new/${row.id}`)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              class="edit2"
              @click="open(row.id)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-check"
              circle
              class="edit3"
              @click="openBox(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagesize"
        :total="total"
        layout="prev,pager, next,sizes, jumper"
      >
      </el-pagination>
    </div>
    <!-- 弹出框 -->
    <questionsPreviews :dialogFormVisible.sync="dialogFormVisible" :info='info'></questionsPreviews>
  </div>
</template>

<script>
import { list, remove, detail, choiceAdd } from '@/api/hmmm/questions.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { simple } from '@/api/hmmm/subjects.js'
import { direction } from '@/api/hmmm/constants.js'
import { userSimple } from '@/api/base/users.js'
import questionsPreviews from '../components/questions-preview.vue'
export default {
  components: {
    questionsPreviews
  },
  data () {
    return {
      input: '',
      tableData: [],
      page: 1,
      pagesize: 10,
      total: 0,
      loading: false,
      form: {
        subjectID: '',
        catalogID: '',
        tags: '',
        keyword: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creatorID: '',
        remarks: '',
        shortName: '',
        province: '',
        city: ''
      },
      rules: {},
      dialogFormVisible: false,
      titleType: 'add',
      id: '',
      citySelect: {
        province: [],
        cityDate: []
      },
      subject: '',
      direction: direction,
      user: '',
      info: ''
    }
  },

  async created () {
    this.initTable()
    this.getCityData()
    // 学科
    const { data } = await simple()
    console.log(data)
    this.subject = data
    // 录入人
    const datas = await userSimple()
    console.log(datas)
    this.user = datas.data
  },

  methods: {
    // 获取省
    getCityData: function () {
      this.citySelect.province = provinces()
    },
    // 选省获取到市
    handleProvince: function (e) {
      this.citySelect.cityDate = citys(e)
      this.form.city = this.citySelect.cityDate[0]
    },
    async initTable () {
      const { data } = await list({
        page: this.page,
        pagesize: this.pagesize
        // subjectID: this.form.subjectID,
        // keyword: this.form.keyword
      })
      console.log(data)
      data.items = data.items.map(item => {
        item.addDate = this.$moment(item.addDate).format('YYYY-MM-DD hh:mm:ss')
        return item
      })
      this.tableData = data.items
      this.total = data.counts
      console.log(this.tableData)
    },
    // pagesize改变时
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.initTable()
    },
    // page改变时
    handleCurrentChange (val) {
      this.page = val
      this.initTable()
    },
    // 清空input
    clearInput () {
      Object.keys(this.form).forEach(item => {
        this.form[item] = null
      })
      this.initTable()
    },
    // 搜索input
    async searchInput () {
      const { data } = await list({
        page: this.page,
        pagesize: this.pagesize,
        ...this.form
      })
      console.log(data)
      this.tableData = data.items
    },
    // 点击新增用户按钮
    async openDialog (row) {
      const { data } = await detail(row)
      console.log(data)
      this.info = data
      this.dialogFormVisible = true
    },
    // 加入精选
    openBox (id) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      })
        .then(async () => {
          await choiceAdd({
            choiceState: 1,
            id
          })
          this.$message({
            type: 'success',
            message: '加入精选成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    // 删除 取消确认按钮
    open (id) {
      this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发删除请求
          await remove({ id })
          this.initTable()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作！'
          })
        })
    }
  },
  computed: {
    diologTitle () {
      if (this.titleType === 'add') {
        return '创建用户'
      } else if (this.titleType === 'edit') {
        return '编辑用户'
      } else {
        return '创建用户'
      }
    }
  }
}
</script>

<style scoped lang="less">
.box-card {
  margin: 20px;
  background-color: #fff;
  padding: 20px;
  .box-header {
    .box-top {
      display: flex;
      justify-content: space-between;
      margin-bottom: 15px;
      div {
        font-size: 12px;
        color: pink;
      }
    }
    .cols {
      .cols-btn {
        margin-left: 60px;
      }
    }
    .header-content {
      background-color: #f4f4f5;
      color: #909399;
      font-size: 13px;
      padding-left: 8px;
      height: 40px;
      line-height: 40px;
      margin-bottom: 20px;
      .el-icon-info {
        margin-right: 5px;
      }
    }
  }
}
.edit1 {
  color: #409eff;
  background: #ecf5ff;
  border-color: #b3d8ff;
  &:hover {
    background-color: #409eff;
    color: #fff;
  }
}
.edit2 {
  color: #f56c6c;
  background: #fef0f0;
  border-color: #fbc4c4;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
.edit3 {
  color: #e6a23c;
  background: #fdf6ec;
  border-color: #f5dab1;
  &:hover {
    background-color: #e6a23c;
    color: #fff;
  }
}
.edit4 {
  color: #67c23a;
  background: #f0f9eb;
  border-color: #c2e7b0;
  &:hover {
    background-color: #67c23a;
    color: #fff;
  }
}
.el-pagination {
  height: 40px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-top: 20px;
}

.el-dialog {
  .el-form {
    width: 400px;
    margin: auto;
    .el-input {
      width: 310px;
      height: 35px;
    }
  }
  .dialog-footer {
    margin-top: 60px;
    text-align: center;
  }
}
</style>
